import { selKeyt, setKeyt  } from '@/services/kety';
import { errNotification, successNotification } from '@/utils/notification';

export default {
    namespace: 'keyt',
  
    state: {
        modalIsShow: false,
        publicKey: false,
        isSet: false
    },
  
    effects: {
      *fetchSelKeyt(param, { call, put }) { //查询我的密钥是否设置
        const response = yield call(selKeyt, param);
        if(response.success){
            yield put({
                type: 'selKeyt',
                payload: true,
            });
        }else{
            yield put({
                type: 'selKeyt',
                payload: false,
            });
        }
      },
      *fetchSetKeyt(param, { call, put }) { //设置我的密钥
        const response = yield call(setKeyt, param)
        if(response.success){
            successNotification("设置成功","")
            let newState = {
                key: response.data.key,
                modalIsShow: false,
            }
            yield put({
                type: 'setKeyt',
                payload: newState,
            });
        }else{
            errNotification("设置失败","")
            let newState = {
                key: false,
                modalIsShow: false,
            }
            yield put({
                type: 'setKeyt',
                payload: newState,
            });
        }
        const response2 = yield call(selKeyt, param);
        if(response2.success){
            yield put({
                type: 'selKeyt',
                payload: true,
            });
        }else{
            yield put({
                type: 'selKeyt',
                payload: false,
            });
        }
      },
    },
  
    reducers: {
      setKeyt(state, action) {
        return {
            ...state,
            modalIsShow: action.payload.modalIsShow,
            publicKey: action.payload.key,
        };
      },
      setPublicKey(state, action) {
        return {
            ...state,
            publicKey: action.payload,
        };
      },
      selKeyt(state, action) {
        return {
          ...state,
          isSet: action.payload
        };
      },
    },
  };